package com.jarvis.app.car;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.jarvis.app.car.common.MessageResult;
import com.jarvis.app.car.constant.AppConstant;
import com.jarvis.app.car.model.Ticket;
import com.jarvis.app.car.model.User;
import com.jarvis.app.car.util.ServiceManager;
import com.jarvis.app.car.util.ThreadPoolManager;
import com.mysql.jdbc.StringUtils;

import java.util.Objects;

public class TicketActivity extends AppCompatActivity {

    private ImageView iv_back;

    private TextView tv_issue;

    private EditText et_answer;

    private Button bt_submit;

    private final Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(@NonNull Message message) {
            MessageResult<Ticket> messageResult = (MessageResult<Ticket>) message.obj;

            if (!messageResult.isJudgeSuccess()) {
                Toast.makeText(TicketActivity.this, messageResult.getMsg(), Toast.LENGTH_SHORT).show();

            } else {
                Ticket result = messageResult.getResult();
                tv_issue.setText("问题：" + result.getIssue());
                if (!TextUtils.isEmpty(result.getAnswer())) {
                    et_answer.setText("回答：" + result.getAnswer());
                } else {
                    et_answer.setHint("回答：暂无（等待管理员反馈）");
                }


            }
        }
    };

    private final Handler answerHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message message) {
            MessageResult<Void> messageResult = (MessageResult<Void>) message.obj;
            if (!messageResult.isJudgeSuccess()) {
                Toast.makeText(TicketActivity.this, messageResult.getMsg(), Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(TicketActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                setResult(Integer.MIN_VALUE);
                finish();
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_ticket);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Intent intent = getIntent();
        long ticketId = intent.getLongExtra("ticketId", 0);


        iv_back = findViewById(R.id.iv_back);
        tv_issue = findViewById(R.id.tv_issue);
        et_answer = findViewById(R.id.et_answer);
        bt_submit = findViewById(R.id.bt_submit);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if (AppConstant.ACCOUNT_SUPER != 1) {
            et_answer.setEnabled(false);
            et_answer.setFocusable(false);
            bt_submit.setVisibility(View.GONE);
        }


        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String answer = et_answer.getText().toString().trim();
                if (StringUtils.isNullOrEmpty(answer)) {
                    Toast.makeText(TicketActivity.this, "回答不能我为空", Toast.LENGTH_SHORT).show();
                }

                ThreadPoolManager.threadPoolExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        MessageResult<Void> one = ServiceManager.ticketService.answer(ticketId, answer);
                        Message msg = Message.obtain();
                        msg.obj = one;
                        answerHandler.sendMessage(msg);
                    }
                });
            }

        });


        ThreadPoolManager.threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                MessageResult<Ticket> updateMessage = ServiceManager.ticketService.getById(ticketId);
                Message msg = Message.obtain();
                msg.obj = updateMessage;
                handler.sendMessage(msg);
            }
        });

    }


}